package com.shinhye.smartsms;

import java.util.Date;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shinhye.smartsms.utils.CommonUtils;
import com.shinhye.smartsms.utils.MyAsyncQueryHandler;
import com.shinhye.smartsms.utils.SmsUris;

public class SearchableActivity extends ListActivity{
	private MyCursorAdapter adapter;
	private MyAsyncQueryHandler asyncQueryHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ListView lv = getListView();
		
		adapter = new MyCursorAdapter(this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		asyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());
		
		lv.setAdapter(adapter);

		Intent intent = getIntent();
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String query = intent.getStringExtra(SearchManager.QUERY);
			doMySearch(query);
		}
		
	}

	private void doMySearch(String query) {
		String[] projection = new String[]{
				"_id",
				"address",
				"body",
				"date"
		};
		
		asyncQueryHandler.startQuery(911, adapter, SmsUris.SMS_URI, projection, "body like ?", new String[]{"%" + query + "%"}, "address");
	}
	
	class MyCursorAdapter extends CursorAdapter {

		public MyCursorAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = View.inflate(context, R.layout.item_conversation, null);
			ViewHolder holder = new ViewHolder();
			
			holder.iv_contact = (ImageView) view.findViewById(R.id.iv_contact);
			holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
			holder.tv_msg = (TextView) view.findViewById(R.id.tv_msg);
			holder.tv_date = (TextView) view.findViewById(R.id.tv_date);
			view.setTag(holder);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ViewHolder holder = (ViewHolder) view.getTag();
			String address = cursor.getString(1);
			String body = cursor.getString(2);
			long date = cursor.getLong(3);
			
			String contactName = CommonUtils.getContactName(getContentResolver(), address);
			if(TextUtils.isEmpty(contactName)) {
				holder.tv_name.setText(address);
				holder.iv_contact.setImageResource(R.drawable.ic_unknow_contact_picture);
			} else {
				holder.tv_name.setText(contactName);
				Bitmap photo = CommonUtils.getContactPhoto(getContentResolver(), address);
				if(photo == null)
					holder.iv_contact.setImageResource(R.drawable.ic_contact_picture);
				else
					holder.iv_contact.setImageBitmap(photo);
			}
			
			holder.tv_msg.setText(body);
			
			String formattedDate;
			if(DateUtils.isToday(date)) {
				formattedDate = DateFormat.getTimeFormat(context).format(new Date(date));
			} else {
				formattedDate = DateFormat.getDateFormat(context).format(new Date(date));
			}
			holder.tv_date.setText(formattedDate);

		}
	}
	
	class ViewHolder {
		ImageView iv_contact;
		TextView tv_name;
		TextView tv_msg;
		TextView tv_date;
	}
}
